// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

/**
 * 编辑块类
 */
@ccclass
export default class GameEdit extends cc.Component {

    // 玩游戏界面下 - 游戏场景父节点
    @property(cc.Node)
    blockParent: cc.Node = null

    // 区块预制体
    @property(cc.Prefab)
    preBlock: cc.Prefab = null

    // 区块精灵图片
    @property([cc.SpriteFrame])
    spfBlockArr: Array<cc.SpriteFrame> = []

    // 添加的区块
    blockArr: Object = null


    btnClick(sender, str){
        if (Object.keys(this.blockArr).includes(str)) {
            this.addBlock(str)
        }else if(str == 'log'){
            this.logStr(str)            
        }else{

        }
    }

    // 添加方块
    addBlock(str:string){
       let block = cc.instantiate(this.preBlock)
       block.parent = this.blockParent
       block.width = this.blockArr[str].w
       block.height = this.blockArr[str].h
       block.attr({blockType:str})
       block.getComponent(cc.Sprite).spriteFrame = this.blockArr[str].spf
    }

    // 打印方块位置
    logStr(str){
        let blockNodes = this.blockParent.children
        let arrBlockType = []
        let arrPos = []
        for (let index = 0; index < blockNodes.length; index++) {
            let block = blockNodes[index]
            
            if (block.name == 'block'){
                // console.log(block['xx'],block['yy']);
                // console.log(block['blockType']);
                arrBlockType.push(block['blockType'])
                arrPos.push('cc.v2('+block['xx']+', '+ block['yy'] +')')
            }
        }   
        console.log(arrBlockType);
        console.log(arrPos.toString());
    }
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.blockArr  = {
            '1_2': {
                w:108,
                h:220,
                spf: this.spfBlockArr[0]
            },
            '1_3': {
                w:108,
                h:332,
                spf: this.spfBlockArr[1]
            },
            '2_1': {
                w:220,
                h:108,
                spf: this.spfBlockArr[2]
            },
            '3_1': {
                w:332,
                h:108,
                spf: this.spfBlockArr[3]
            },
        }
    }

    start () {

    }

    // update (dt) {}
}
